package main

import (
	"fmt"
	"strings"
	"time"
)

var timeFormat = "2006-01-02 15:04:05"

func ParseStringToTime(str string) (time.Time, error) {
	loc, err := time.LoadLocation("Local")
	if err != nil {
		return time.Now(), err
	}
	t, err2 := time.ParseInLocation(timeFormat, str, loc)
	if err2 != nil {
		return time.Now(), err2
	}

	return t, nil
}
func functionA() {
	createtime := int64(1474992000)
	date := strings.Replace(strings.Split(time.Unix(createtime, 0).Format("2006-01-02 15:04:05"), " ")[0], "-", "", -1)
	fmt.Println("createtime:", date)
}
func main() {
	//时间戳
	//当前时间戳
	fmt.Println(time.Now().Unix()) //# 1389058332

	//str格式化时间 ,当前格式化时间
	//这是个奇葩,必须是这个时间点, 据说是go诞生之日, 记忆方法:6-1-2-3-4-5
	fmt.Println(time.Now().Format("2006-01-02 15:04:05")) //# 2014-01-07 09:42:20

	//时间戳转str格式化时间
	str_time := time.Unix(1389058332, 0).Format("2006-01-02 15:04:05")
	fmt.Println(str_time) //# 2014-01-07 09:32:12

	//str格式化时间转时间戳
	//这个比较麻烦
	the_time := time.Date(2014, 1, 7, 5, 50, 4, 0, time.Local)
	unix_time := the_time.Unix()
	fmt.Println(unix_time) //# 389045004

	// 还有一种方法,使用time.Parse
	the_time, err := time.Parse("2006-01-02 15:04:05", "2016-09-27 23:59:59")
	if err == nil { //# 1389171881
		unix_time := the_time.Unix()
		fmt.Println("unix_time:", unix_time)
	}
	the_time, _ = ParseStringToTime("2016-09-28 00:00:00")
	fmt.Println("ParseStringToTime:", the_time.Unix())
	functionA()
}
